package com.szy.web.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class ClientUserDao {
	SqlManager manager;
	String sql = "";
	ResultSet rs;
	
	public ClientUserDao() throws IOException, ClassNotFoundException, SQLException
	{
		manager = SqlManager.createInstance();
	}
	
	/**
	 * 验证用户名和密码是否正确
	 * @param uname 用户名
	 * @param password 密码
	 * @return
	 * @throws SQLException
	 */
	public boolean validate(String uname,String password) throws SQLException
	{
		boolean result = true;
		sql = "select count(username) from clientUser where class=?";
		Object[] params = new Object[]{uname};
		manager.connectDB();
		rs = manager.executeQuery(sql, params);
		if (rs.next()&&rs.getLong(1)==1)
		{
			result = false;
		}
		manager.closeDB();
		return result;
	}
	
	/**
	 * 更新用户信息，
	 * 注意：只能根据用户名更新密码
	 * @param uname 用户名
	 * @param password 新密码
	 */
	public boolean addCleintUser(String uname,String password) throws SQLException
	{
		sql = " INSERT INTO clientUser(class,username) VALUES(?,?)";
		Object[] params = new Object[]{uname,password};
		manager.connectDB();
		return manager.executeUpdate(sql, params);
	}
	public void update(String uname,String password)
	{
		throw new NotImplementedException();
	}
	
	public boolean canSendMsg(String username)
	{
		sql = " select * from clientUser where class = ?";
		Object[] params = new Object[]{username};
		try {
			manager.connectDB();
			rs = manager.executeQuery(sql, params);
			while (rs.next()) 
			{
				int flag = rs.getInt(4);
				System.out.println( "flag = "+ flag );
				if (flag == 1) 
				{
					String beginTimeString = rs.getString(5);
					int  timeLenth= rs.getInt(6);
					if ( timeLenth == -1 )
					{
						manager.closeDB();
						return true;//永远禁言
					}
					SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date nowDate = new Date();
					String nowtimeString = dfs.format(nowDate);
					Date begintimeDate = null;
					Date endData = null;
					try {
						begintimeDate = dfs.parse(beginTimeString);
						endData = dfs.parse(nowtimeString);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					long between=(endData.getTime()-begintimeDate.getTime())/1000;
					
					long hour = 0;
					if ((between%3600) == 0 )
					{
						hour = between/3600;
					}
					else 
					{
						hour = between/3600 + 1;
					}
					System.out.println( "时间差 = "+ hour );
					if ( hour > timeLenth )
					{
						sql = "update clientUser set disableSendMsg = 0 ,disableTime = \"\",timeLength = 0 where class =?";
						Object[] params2 = new Object[]{ username};
						manager.executeUpdate(sql, params2);
						manager.closeDB();
						return false;
					}
					else 
					{
						manager.closeDB();
						return true;
					}
				}
				else 
				{
					manager.closeDB();
					return false;
				}
			
			}
			manager.closeDB();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return true;
	}
	public boolean setSendMsgSatus(String usernamString ,int timeLength) 
	{
		
		try 
		{
			manager.connectDB();
			if (timeLength == 0 ) 
			{
				sql = "update clientUser set disableSendMsg = 0 ,disableTime = \"\",timeLength = 0 where class =?";
				Object[] params = new Object[]{usernamString};
				boolean reslut = manager.executeUpdate(sql, params);
				manager.closeDB();
				return reslut;
			}
			else 
			{
				if ( timeLength != -1 )
				{
					timeLength *= 24;
				}
				SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date nowDate = new Date();
				String nowtimeString = dfs.format(nowDate);	
				sql = "update clientUser set disableSendMsg = 1 ,disableTime = ?,timeLength = ? where class =?";
				Object[] params = new Object[]{ nowtimeString,timeLength,usernamString};
				boolean reslut = manager.executeUpdate(sql, params);
				manager.closeDB();
				return reslut;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;
	}
}
